// pages/interface/index.js
Page({

  data: {
    books: [],
    keywords: '',
    history: wx.getStorageSync('history') || []
  },

  getBooks() {
    wx.showLoading({
      title: "正在拼命加载中...",
      mask: true,
    });
    wx.request({
      url: 'https://hmajax.itheima.net/api/books',
      method: 'GET',
      data: { creator: 'zhangsan' },
      success: (res) => {
        console.log(res);
        this.setData({ books: res.data.data }, () => {
          wx.showToast({
            title: '获取书单成功',
            duration: 2000,
            mask: true,
            icon: 'none'
          })
        });
      },
      fail: (e) => {
        console.log(e);
        wx.hideLoading();
      }
      ,
    })
  },
  //   showModel() {
  //     wx.showModal({
  //       title: 'title',
  //       content:'您确定要删除吗？',
  //     }),
  //  }
  // 2. 展示模态框
  showModel() {
    wx.showModal({
      title: "温馨提示",
      content: "您确定要删除吗?",
      complete: (res) => {
        if (res.cancel) {
        }

        if (res.confirm) {
        }
      },
    });
  },
  showActionSheet() {
    wx.showActionSheet({
      itemList: [],

    })
  },
  async saveData() {
    // wx.setStorageSync('token', '1111111111');
    // 异步存储
    await wx.setStorage({
      key: 'token',
      data: '111111111'
    });

  },
  readData() {
    wx.showToast({
      title: wx.getStorageSync('token')
    })
  },
  removeData() {
    wx.removeStorageSync('token');
  },
  clearData() {
    wx.clearStorageSync();
  },
  async chooseImage() {
    // 读取结果
    try {
      const result = await wx.chooseMedia({
        count: 1, // 只选取一张图片
        mediaType: ['image'] // 只选取图片
      })
      console.log(result)
    } catch (error) {
      console.log(error);
    }
  },
  onSearch() {
    if (this.data.keywords.trim === "") {
      wx.showToast({
        title: '请输入搜索内容',
        icon: 'none'
      })
      return;
    }
    const tempArr = this.data.history;
    tempArr.unshift(this.data.keywords);
    this.setData({
      history: tempArr,
      keywords: ''
    }, () => {
      wx.setStorageSync('history', this.data.history);
    });

  },
  onClear() {
    wx.clearStorageSync();
  },
  clickMe(event) {
    console.log(event.target.dataset);
  },
  clickMeTwo(event) {
    console.log(event.mark);
  }
});